//
//  ImageNewsViewController.m
//  项目一TimeMovie
//
//  Created by apple on 16/4/1.
//  Copyright © 2016年 huangmingyu. All rights reserved.
//

#import "ImageNewsViewController.h"
#import "ImageModel.h"
#import "NewsImageViewCell.h"
#import "photoViewController.h"

#define kItemWidth 80
#define kItemHeight 66
#define kItemCount 4


#define kItemSpace (kScreenWidth - kItemWidth * kItemCount)/ (kItemCount + 5)


@interface ImageNewsViewController () {
    
    NSMutableArray *dataArray;
}

@end

@implementation ImageNewsViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"图片";
//    [self.ImageWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.qq.com"]]];
    
    //数据加载
    [self loadData];
    //视图的配置
    [self configUI];
}


- (void)loadData {
    
    dataArray = [NSMutableArray array];
    
    NSArray *array = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_list" ofType:@"json"]] options:NSJSONReadingMutableContainers error:nil];
    
    for (NSDictionary *dic in array) {
        
        ImageModel *model = [[ImageModel alloc] init];
        
        model.type = [dic[@"type"] integerValue];
        model.imgView = dic[@"image"];
        model.imageId = [dic[@"id"] integerValue];
        
        [dataArray addObject:model];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return dataArray.count;
}

- (void)configUI {
    
    self.CollectionFlowLayout.itemSize = CGSizeMake(kItemWidth, kItemHeight);
//    self.CollectionFlowLayout.minimumLineSpacing = kItemSpace;
    self.CollectionFlowLayout.minimumInteritemSpacing = kItemSpace ;
    self.CollectionFlowLayout.sectionInset = UIEdgeInsetsMake(kItemSpace, kItemSpace, 0, kItemSpace);

    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NewsImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"789" forIndexPath:indexPath];
    
    cell.model = dataArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    photoViewController *photo = [[photoViewController alloc] init];
    
    photo.selectIndexPath = indexPath;
    
    NSMutableArray *array = [NSMutableArray array];
    for (ImageModel *model in dataArray) {
        [array addObject:model.imgView];
    }
    
    photo.imageURLArr = array;
    
    [self.navigationController pushViewController:photo animated:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
